using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Configuration;
using PeoplesJukebox.Services.ServiceModel.ErrorHandlers;

namespace PeoplesJukebox.Services.ServiceModel.Behaviors
{
    /// <summary>
    /// Service behavior for logging all exceptions
    /// </summary>
    public class LogErrorHandlerBehavior : BehaviorExtensionElement, IServiceBehavior
    {
        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            //do nothing
        }

        /// <summary>
        /// Adds a <see cref="LogErrorHandler"/> to each dispatcher
        /// </summary>
        /// <param name="serviceDescription">Service description</param>
        /// <param name="serviceHostBase">Service host</param>
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
            {
                dispatcher.ErrorHandlers.Add(new LogErrorHandler());
            }
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            //do nothing
        }

        public override Type BehaviorType
        {
            get { return typeof(LogErrorHandlerBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new LogErrorHandlerBehavior();
        }
    }
}